import {closeInfoWin} from "@/utils/map/mapTools3x";

/**
 * 点位点击事件 - 入海河流
 * @param map 地图实例
 * @param executeClickEmits 事件处理函数
 * @returns {Promise<unknown>}
 */
export const useMapPointClick = (map, executeClickEmits) => {
  return map.on('click', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
      return feature;
    }, {hitTolerance: 10});
    closeInfoWin();
    if (feature) {
      const res = feature.getProperties();
      if (res.type === 'pointlist') {
        const item = res.item;
        executeClickEmits(item);
      }
    }
  });
}

/**
 * 点位点击事件 - 近岸海域
 * @param map 地图实例
 * @param executeClickJahyEmits
 * @returns {Promise<unknown>}
 */
export const useMapPointClickJahy = (map, executeClickJahyEmits) => {
  return map.on('click', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
      return feature;
    }, {hitTolerance: 10});
    closeInfoWin();
    if (feature) {
      const res = feature.getProperties();
      if (res.type === 'pointlist') {
        const item = res.item;
        executeClickJahyEmits(item);
      }
    }
  });
}

/**
 * 点击入海河流点位进行的事件处理
 */
export const useRhhlClickExecuteEvent = (self, data, feature) => {
  console.log(feature.lx);
  switch (feature.lx.trim()) {
    case 'b_gy':
      if (self.$refs.dbssz) {
        self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
      }
      self.$refs.mapPopBox.mapPopDialog = true
      self.$refs.mapPopBox.mapPopInfo = feature
      self.$refs.mapPopBox.mapPopType = 'bdh_gyqyTan'
      break;
    case 'b_jz':
      if (self.$refs.dbssz) {
        self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
      }
      self.$refs.mapPopBox.mapPopDialog = true
      self.$refs.mapPopBox.mapPopInfo = feature
      self.$refs.mapPopBox.mapPopType = 'bdh_wsclc'
      break;
    case '国控':
    case '国考':
    case '其他':
    case '省考':
    case '':
      if (self.$refs.dbssz) {
        self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
      }
      self.$refs.mapPopBox.mapPopDialog = true
      self.$refs.mapPopBox.mapPopInfo = feature
      self.$refs.mapPopBox.mapPopType = 'dhcSzzdz'
      break;
    case 'b_1xq':
      window.open(`${data.tb}?code=${data.code}&type=N&ids=${feature.pkId}&zhis=3&sfdl=false`)
      break;
    case 'b_2xq':
      window.open(`${data.tb}?code=${data.code}&type=&ids=&zhis=3&sfdl=false`)
      break;
    case 'b_sc':
      window.open(`${data.tb}?code=${data.code}&type=SCYZ&ids=${feature.pkId}&zhis=3&sfdl=false`)
      break;
    case 'b_cz':
      window.open(`${data.tb}?code=${data.code}&type=&ids=&zhis=3&sfdl=false`)
      break;
    case 'b_pk':
      if (self.$refs.dbssz) {
        self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
      }
      self.$refs.mapPopBox.mapPopDialog = true
      self.$refs.mapPopBox.mapPopInfo = feature
      self.$refs.mapPopBox.mapPopType = 'bdh_rhpwk'
      break;
    case 'bdh_shfxy':
      if (self.$refs.dbssz) {
        self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
      }
      self.$refs.mapPopBox.mapPopDialog = true
      self.$refs.mapPopBox.mapPopInfo = feature
      self.$refs.mapPopBox.mapPopType = 'bdh_shfxy'
      break;
    default:
      if (feature.lx == '河流') {
        if (self.$refs.dbssz) {
          self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
        }
        self.$refs.mapPopBox.mapPopDialog = true
        self.$refs.mapPopBox.mapPopInfo = feature
        self.$refs.mapPopBox.mapPopType = 'bdh_dmxx'
      }
  }
}

/**
 * 点击近岸海域点位
 * @param self 当前实例
 * @param item 点位信息
 */
export const useExecuteClickJahyEmits = (self, item) => {
  if (item.lx === 'jahy_hsyc' || item.lx === 'jahy_ycwwhy' || item.lx === 'jahy_qhdghy' || item.lx === 'jahy_zdhkhy' || item.lx === 'jahy_ychzbhxqxjc' || item.lx === 'jahy_jahy') {
    self.$refs.mapPopBox.mapPopDialog = true
    self.$refs.mapPopBox.mapPopInfo = item
    self.$refs.mapPopBox.mapPopType = 'bdh_jahy_point'
    item.lx === 'jahy_jahy' ? self.$refs.mapPopBox.pointType = 'pointJasz' : self.$refs.mapPopBox.pointType = 'defaultPoint'
  }
}

/**
 * 点击近岸海域 - 海水浴场
 * @param self
 * @param item
 */
export const useToggleHsycPointHandler = (self, item) => {
  self.bdhHsycDwmcName !== item.dwmc ? self.bdhHsycDwmcName = item.dwmc : self.bdhHsycDwmcName = ''
  if (self.bdhHsycDwmcName && self.bdhHsycDwmcName !== '') {
    self.$refs.mapPopBox.mapPopDialog = true
    self.$refs.mapPopBox.mapPopInfo = item
    self.$refs.mapPopBox.mapPopType = 'bdh_jahy_point'
    self.$refs.mapPopBox.pointType = 'defaultPoint'
  } else {
    // 未选择点位
    self.$refs.mapPopBox.mapPopDialog = false
    self.$refs.mapPopBox.mapPopInfo = []
  }
}

/**
 * 点击断面点位弹窗
 * @param self
 * @param map
 * @returns {*}
 */
export const useClickDmMapPoint = (self, map) => {
  return map.on('click', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) {
      return feature;
    }, {hitTolerance: 10});
    closeInfoWin();
    if (feature) {
      const res = feature.getProperties();
      if (res.type === 'pointlist') {
        const item = res.item;
        if (self.$refs.dbssz) {
          self.$refs.dbssz.mapsz = self.$refs.dbssz.sectionActive
        }
        self.$refs.mapPopBox.mapPopDialog = true
        self.$refs.mapPopBox.mapPopInfo = item
        self.$refs.mapPopBox.mapPopType = 'dhcHys'
      }
    }
  });
}
